package com.zbkj.crmeb.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.PageParamRequest;
import com.exception.CrmebException;
import com.github.pagehelper.PageHelper;
import com.zbkj.crmeb.user.dao.UserLevelGoodDao;
import com.zbkj.crmeb.user.model.UserLevelGood;
import com.zbkj.crmeb.user.request.UserLevelGoodRequest;
import com.zbkj.crmeb.user.request.UserLevelGoodSearchRequest;
import com.zbkj.crmeb.user.service.UserLevelGoodService;

import java.util.List;
import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * UserLevelServiceImpl 接口实现 +---------------------------------------------------------------------- | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +---------------------------------------------------------------------- | Copyright (c) 2016~2020 https://www.crmeb.com All rights
 * reserved. +---------------------------------------------------------------------- | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +---------------------------------------------------------------------- | Author: CRMEB Team <admin@crmeb.com>
 * +----------------------------------------------------------------------
 */
@Service
public class UserLevelGoodServiceImpl extends ServiceImpl<UserLevelGoodDao, UserLevelGood> implements UserLevelGoodService {

    @Resource
    private UserLevelGoodDao dao;

    @Override
    public List<UserLevelGood> getList(UserLevelGoodSearchRequest request, PageParamRequest pageParamRequest) {
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        return dao.selectList(null);
    }

    @Override
    public void addData(UserLevelGoodRequest paramVO) {
        // 检查级别重复
        checkRepeatLevelSort(paramVO);
        UserLevelGood data = new UserLevelGood();
        BeanUtils.copyProperties(paramVO, data);
        dao.insert(data);
    }

    @Override
    public void updateData(UserLevelGoodRequest paramVO) {
        // 检查级别重复
        checkRepeatLevelSort(paramVO);
        UserLevelGood data = new UserLevelGood();
        BeanUtils.copyProperties(paramVO, data);
        dao.updateById(data);
    }

    // 检查级别重复
    private void checkRepeatLevelSort(UserLevelGoodRequest paramVO) {
        LambdaQueryWrapper<UserLevelGood> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserLevelGood::getLevelSort, paramVO.getLevelSort());
        if (paramVO.getId() != null) {
            wrapper.ne(UserLevelGood::getId, paramVO.getId());
        }
        UserLevelGood userLevelGood = dao.selectOne(wrapper);
        if (userLevelGood != null) {
            throw new CrmebException("同级别商品已存在，请重新设置级别");
        }
    }

    @Override
    public void delete(Integer id) {
        dao.deleteById(id);
    }

    @Override
    public UserLevelGood getByGoodId(Integer productId) {
        LambdaQueryWrapper<UserLevelGood> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserLevelGood::getGoodId, productId);
        return dao.selectOne(wrapper);
    }
}

